package apptoko.com;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import apptoko.com.background.BackgroundTasks;
import apptoko.com.background.ContentNetworkInterface;
import apptoko.com.util.Constants;
import apptoko.com.util.Utils;

import com.appota.asdk.commons.AccessTokenUtil;
import com.google.android.gcm.GCMBaseIntentService;

public class GCMIntentService extends GCMBaseIntentService {
	
	private static final String TAG = "GCMIntentService";
	private String accessToken;
	//private ContentNetworkInterface cni;
	public static final String DISPLAY_MESSAGE_ACTION = "apptoko.com.pushnotifications.DISPLAY_MESSAGE";
	private BackgroundTasks bgTask;
	 
    public GCMIntentService() {
        super(ContentNetworkInterface.SENDER_ID);
       // cni = ContentNetworkInterface.getInstance(this);
    }
 
    /**
     * Method called on device registered
     **/
    @Override
    protected void onRegistered(Context context, String registrationId) {
    	Log.i(TAG, "Device Registered");
        accessToken = ((AppStoreVnApplication)getApplication()).getAccessToken();
        if(null == accessToken || accessToken.equals(null)){
			accessToken = AccessTokenUtil.getInstance().init(this).getSavedAccessToken();
        }
        SharedPreferences pref = getSharedPreferences(Constants.SHARED_NAME, MODE_PRIVATE);
        String allowPush = pref.getString(SettingsActivity.KEY_ALLOW_PUSH, "1");
        boolean isRooted1 = ((AppStoreVnApplication) getApplication()).isRooted();
        boolean isRooted = Utils.isRootedStatic(context, isRooted1);
        bgTask = new BackgroundTasks(context);
        try {
			bgTask.registerGCM(accessToken, registrationId, allowPush, isRooted, 0);
		} catch (NameNotFoundException e) {
			e.printStackTrace();
		}
    }
 
    /**
     * Method called on device un registred
     * */
    @Override
    protected void onUnregistered(Context context, String registrationId) {
        Log.i(TAG, "Device unregistered");
        //CommonUtilities.displayMessage(context, getString(R.string.gcm_unregistered));
        bgTask = new BackgroundTasks(context);
        bgTask.unregisterGCM(registrationId);
    }
 
    /**
     * Method called on Receiving a new message
     * */
    @Override
    protected void onMessage(Context context, Intent intent) {
        Log.e(TAG, "Received message");
        String message = intent.getExtras().getString("message");
        String title = intent.getExtras().getString("title");
        String linkUrl = intent.getExtras().getString("link_url");
        String store = intent.getExtras().getString("store");
        String contentId = intent.getExtras().getString("content_id");
        
        Bundle data = new Bundle();
        data.putString("message", message);
        data.putString("title", title);
        data.putString("link_url", linkUrl);
        data.putString("store", store);
        data.putString("content_id", contentId);
        sendPushData(context, data);
    }
    
    public void sendPushData(Context context, Bundle data) {
        Intent intent = new Intent(DISPLAY_MESSAGE_ACTION);
        intent.putExtras(data);
        context.sendBroadcast(intent);
    }
 
    /**
     * Method called on receiving a deleted message
     * */
    @Override
    protected void onDeletedMessages(Context context, int total) {
        Log.i(TAG, "Received deleted messages notification");
    }
 
    /**
     * Method called on Error
     * */
    @Override
    public void onError(Context context, String errorId) {
        Log.i(TAG, "Received error: " + errorId);
    }
 
    @Override
    protected boolean onRecoverableError(Context context, String errorId) {
        // log message
        Log.i(TAG, "Received recoverable error: " + errorId);
        return super.onRecoverableError(context, errorId);
    }
}
